package com.zhenzhang.myblog.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
public class AssetsManager {
    private static final Path WORK_PATH = Paths.get("").toAbsolutePath();
    private static final Path ASSETS_ROOT = Paths.get("assets").toAbsolutePath();
    private static final Path AVATAR_ROOT = Paths.get(ASSETS_ROOT + "/avatar");
    private static final Path COVER_ROOT = Paths.get(ASSETS_ROOT + "/article/cover");
    private static final Path ARTICLE_IMAGE_ROOT = Paths.get(ASSETS_ROOT + "/article/image");
    static {
        try {
            if (!Files.exists(COVER_ROOT)) {
                Files.createDirectories(COVER_ROOT);
                log.info("创建封面存储目录：" + COVER_ROOT);
            }
            if (!Files.exists(AVATAR_ROOT)) {
                Files.createDirectories(AVATAR_ROOT);
                log.info("创建头像存储目录：" + AVATAR_ROOT);
            }
            if (!Files.exists(ARTICLE_IMAGE_ROOT)) {
                Files.createDirectories(ARTICLE_IMAGE_ROOT);
                log.info("创建文章图片内容存储目录：" + ARTICLE_IMAGE_ROOT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取用户头像文件在系统中的文件路径
     * @param avatarName    用户头像文件名
     * @return  路径
     */
    public static Path getUserAvatarPath(String avatarName) {
        return Paths.get(ASSETS_ROOT + "/avatar/" + avatarName);
    }

    /**
     *  获取文章封面文件在系统中的文件路径
     * @param coverName     文章封面文件名
     * @return  路径
     */
    public static Path getArticleCoverPath(String coverName) {
        return Paths.get(ASSETS_ROOT + "/article/cover/" + coverName);
    }

    /**
     *  获取文章image内容在系统中的文件路径
     * @param imageName     图片文件名
     * @return  路径
     */
    public static Path getArticleImagePath(String imageName, Integer id) {
        return Paths.get(ASSETS_ROOT + "/article/image/article_" + id + "/" + imageName);
    }

    /**
     * 根据id获取文章图片文件夹路径
     * @param id    文章id
     * @return  图片文件夹路径
     */
    public static Path getArticleImagePath(Integer id) {
        return Paths.get(ASSETS_ROOT + "/article/image/article_" + id);
    }
}
